//
//  encryptbridge.c
//  gdemm
//
//  Created by 乔明超 on 16/6/17.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#include "encryptbridge.h"


static void _decode_str(const char* str, char* out);
/**
 *  解密二维码信息
 *
 *  @param str 为解密的二维码信息
 *
 *  @return 解密后的二维码信息
 */
char * decryptQRcode(char *str){

    char out_buffer[1024];
    _decode_str(str, out_buffer);
    
    return out_buffer;
}
static int _smash_file(const char* file_path)
{
#define _WRITE_UNIT_SIZE                8 * 1024
    char buf[_WRITE_UNIT_SIZE] = {0};
    int count = 0;
    
    for (count = 0; count < 8; ++count)
    {
        hfile_off_t file_size;
        hfile_off_t writed_size = 0;
        hfile_t file = hfile_open(file_path, HOA_WRITE, HOF_OPEN);
        if (HF_INVALID_FILE == file)
        {
            return 1;
        }
        
        file_size = hfile_getsize(file);
        while (writed_size < file_size)
        {
            int write_size = (int)((file_size - writed_size > _WRITE_UNIT_SIZE) ? _WRITE_UNIT_SIZE : (file_size - writed_size));
            
            hfile_write(file, buf, write_size);
            
            writed_size += write_size;
        }
        
        hfile_sync(file);
        hfile_close(file);
    }
    
    return hfile_delete(file_path);
#undef _WRITE_UNIT_SIZE
}


static unsigned int gs_key[4] = {0xac6033e7, 0x00, 0x00, 0x90a23b56};

static void _encode_str(const char* str, char* out)
{
    char tea_buff[1024];
    int tea_size = sizeof(tea_buff);
    int size = 0;
    char tmp_buf[1024] = {0};
    int len = strlen(str);
    len = len / TEA_UNITSIZE + (len % TEA_UNITSIZE) ? TEA_UNITSIZE : 0;
    
    strcpy(tmp_buf, str);
    htea_enc_buff_naked((const char*)tmp_buf, len, gs_key, tea_buff, &tea_size);
    hbase64_encode(tea_buff, tea_size, out, &size);
    
    out[size] = 0;
}

static void _decode_str(const char* str, char* out)
{
    char tea_buff[1024];
    int tea_size = 0;
    int size = 0;
    
    hbase64_decode((unsigned char*)str, tea_buff, &tea_size);
    if (0 == (tea_size % TEA_UNITSIZE))
    {
        size = sizeof(tea_buff);
        htea_dec_buff_naked((const char*)tea_buff, tea_size, gs_key, out, &size);
    }
    
    out[size] = 0;
}